package yicheng.dinnertogetherapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.widget.Toast;

public class BackgroundDateUpdateService extends Service{

	BroadcastReceiver broadcastReceiver;

	public static AmazonClientManager clientManager;

	static SharedPreferences local_user_information;
	SharedPreferences.Editor local_user_editor;
	String PREFS_NAME = "LocalUserInfo";

	String PHONE_NUMBER, NICKNAME;

	String START_TIME;
	String END_TIME;
	Date FROM_DATE;
	Date UNTIL_DATE;

	private void updateFromDateDisplay(){


		Calendar currentCal = Calendar.getInstance();

		int curYear = currentCal.get(Calendar.YEAR);
		int curMonth = currentCal.get(Calendar.MONTH);
		int curDay = currentCal.get(Calendar.DAY_OF_MONTH);
		int curHour = currentCal.get(Calendar.HOUR_OF_DAY);
		int curMin = currentCal.get(Calendar.MINUTE);

		Date currentDate = new Date(curYear, curMonth, curDay , curHour , curMin);

		String fromYear = local_user_information.getString("start_year", "0000");
		String fromMonth = local_user_information.getString("start_month", "00");
		String fromDay = local_user_information.getString("start_day", "00");
		String fromHour = local_user_information.getString("start_hour", "00");
		String fromMin = local_user_information.getString("start_min", "00");


		FROM_DATE = new Date(Integer.parseInt(fromYear), Integer.parseInt(fromMonth), Integer.parseInt(fromDay), Integer.parseInt(fromHour), Integer.parseInt(fromMin));


		String untilYear = local_user_information.getString("end_year", "0000");
		String untilMonth = local_user_information.getString("end_month", "00");
		String untilDay = local_user_information.getString("end_day", "00");
		String untilHour = local_user_information.getString("end_hour", "00");
		String untilMin = local_user_information.getString("end_min", "00");


		UNTIL_DATE = new Date(Integer.parseInt(untilYear), Integer.parseInt(untilMonth), Integer.parseInt(untilDay), Integer.parseInt(untilHour), Integer.parseInt(untilMin));


		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd h:mm a");
		SimpleDateFormat dayOfWeekFormat = new SimpleDateFormat("EE");
		Date dayOfWeekDate = new Date(curYear, curMonth, curDay - 1);

		String dayOfWeekText = dayOfWeekFormat.format(dayOfWeekDate);


		String current_time = dateFormat.format(currentDate);





		if (FROM_DATE.before(currentDate)){

			//from_date_textView.setText(dayOfWeekText + ", " + START_TIME);
			//START_TIME = dateFormat.format(FROM_DATE);
			START_TIME = dayOfWeekText + ", " + current_time;
			//MyInfoActivity.from_date_textView.setText(START_TIME);

			local_user_editor = local_user_information.edit();
			local_user_editor.putString("start_year", String.valueOf(curYear));
			local_user_editor.putString("start_month", String.valueOf(curMonth));
			local_user_editor.putString("start_day", String.valueOf(curDay));
			local_user_editor.putString("start_hour", String.valueOf(curHour));
			local_user_editor.putString("start_min", String.valueOf(curMin));
			local_user_editor.putString("start_time", START_TIME);
			local_user_editor.commit();

			//FROM_DATE= new Date(curYear, curMonth, curDay , curHour, curMin);
			updateStartTime();


		}
		/*	else{
			START_TIME = local_user_information.getString("start_time", dayOfWeekText + ", " + current_time);

			//MyInfoActivity.from_date_textView.setText(START_TIME);

		}*/


		if (UNTIL_DATE.before(currentDate)){
			//until_date_textView.setText(dayOfWeekText + ", " + START_TIME);

			END_TIME = dayOfWeekText + ", " + current_time;
			/*MyInfoActivity.until_date_textView.setText(END_TIME);*/


			local_user_editor = local_user_information.edit();
			local_user_editor.putString("end_year", String.valueOf(curYear));
			local_user_editor.putString("end_month", String.valueOf(curMonth));
			local_user_editor.putString("end_day", String.valueOf(curDay));
			local_user_editor.putString("end_hour", String.valueOf(curHour));
			local_user_editor.putString("end_min", String.valueOf(curMin));
			local_user_editor.putString("end_time", END_TIME);
			local_user_editor.commit();

			//UNTIL_DATE = new Date(curYear, curMonth, curDay , curHour, curMin);

			updateEndTime();


		}
		/*else{
			END_TIME = local_user_information.getString("end_time", dayOfWeekText + ", " + current_time);

			MyInfoActivity.until_date_textView.setText(END_TIME);
		}*/



	}




	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		Toast.makeText(this , "background service test create", Toast.LENGTH_SHORT).show();
		super.onCreate();
	}




	@Override

	public int onStartCommand(Intent intent, int flags, int startId){
		// START YOUR TASKS
		clientManager = new AmazonClientManager();
		local_user_information =  this.getSharedPreferences(PREFS_NAME,0);
		Toast.makeText(this , "background serice test start", Toast.LENGTH_SHORT).show();
		broadcastReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context ctx, Intent intent)
			{

				if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0){
					updateFromDateDisplay();

				}

			}
		};

		registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		// STOP YOUR TASKS


		Toast.makeText(this, "background service test stop", Toast.LENGTH_SHORT).show();

		super.onDestroy();
	}


	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	


	private void updateStartTime(){
		PHONE_NUMBER = local_user_information.getString("PhoneNumber", "default");

		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				SimpleDB.updateSingleAttribute(clientManager, PHONE_NUMBER,SimpleDB.AVAILABILITY_ITEM, SimpleDB.START_TIME_ATTRIBUTE, START_TIME);

			}

		}).start();



	}


	private void updateEndTime(){
		PHONE_NUMBER = local_user_information.getString("PhoneNumber", "default");



		// TODO Auto-generated method stub
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub

				SimpleDB.updateSingleAttribute(clientManager, PHONE_NUMBER,SimpleDB.AVAILABILITY_ITEM, SimpleDB.END_TIME_ATTRIBUTE, END_TIME);
			}

		}).start();



	}
	
	
	

}
